﻿using System;
using ITViet.Web.App_Code;
using DevExpress.Web.ASPxGridView;
using ITViet.Services;
using ITViet.Domains;
using System.Web.UI.HtmlControls;

namespace ITViet.Web.Admin
{
  public partial class CustomerContact : AdminBasePage
  {
    #region Properties
    private string m_innerError = string.Empty;

    internal override int RequiredPermission
    {
      get
      {
        return (int)FunctionList.CustomerContact;
        //return -1;
      }
    }

    private string OpinionType
    {
      get
      {
        string type = string.Empty;
        if (Request.QueryString["t"] != null)
          type = Request.QueryString["t"].ToUpper();
        return type;
      }
    }
    #endregion

    #region Methods

    private void InitPage()
    {
      if (string.IsNullOrEmpty(OpinionType))
        Response.Redirect("CustomerContact.aspx?t=o");
    }

    private void LoadPage()
    {
      DateTime now = DateTime.Now;
      dexFromDate.Value = new DateTime(now.Year, now.Month, 1);
      now = now.AddMonths(1);
      dexToDate.Value = now.AddDays(-now.Day);
    }

    private void LoadList()
    {
      DateTime? fromDate = null;
      DateTime? toDate = null;
      if (dexFromDate.Value != null)
        fromDate = dexFromDate.Date;
      if (dexToDate.Value != null)
        toDate = dexToDate.Date;

      gvList.DataSource = new CustomerOpinionService().GetBySearch(fromDate, toDate, OpinionType, null);
      gvList.DataBind();
    }

    private string DeleteOpinion(int opinionId)
    {
      try
      {
        new CustomerOpinionService().Delete(opinionId);
      }
      catch (Exception ex)
      {
        WriteLog("CustomerContact.aspx::Delete Opinion", ex);
        return Resources.Messages.ERR0004;
      }
      return string.Empty;
    }

    private string ChangeStatus(int id, bool show)
    {
      try
      {
        CustomerOpinion op = new CustomerOpinionService().GetById(id);
        op.IsActive = show;
        new CustomerOpinionService().Save(op);
      }
      catch (Exception ex)
      {
        WriteLog("CustomerContact.aspx::Change IsActive Status", ex);
        return Resources.Messages.ERR0004;
      }
      return string.Empty;
    }

    #endregion

    #region Event

    protected void Page_Load(object sender, EventArgs e)
    {
      InitPage();
      if (!IsCallback && !IsPostBack)
      {
        LoadPage();
        LoadList();
      }
      if (gvList.IsCallback)
        LoadList();
    }

    protected void gvList_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
      ASPxGridView grid = sender as ASPxGridView;
      int opinionId = 0;
      int.TryParse(e.Keys["Id"].ToString(), out opinionId);
      m_innerError = DeleteOpinion(opinionId);
      e.Cancel = true;
      grid.CancelEdit();
      LoadList();
    }

    protected void gvList_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }


    protected void gvList_HtmlDataCellPrepared(object sender, ASPxGridViewTableDataCellEventArgs e)
    {
      if (e.DataColumn.FieldName != "IsActive") return;
      ASPxGridView grid = sender as ASPxGridView;
      HtmlInputCheckBox ckb = grid.FindRowCellTemplateControl(e.VisibleIndex, e.DataColumn, "ckbox") as HtmlInputCheckBox;
      if (ckb != null)
      {
        if ((bool)e.CellValue == true) ckb.Checked = true;
        int id = 0;
        int.TryParse(grid.GetRowValues(e.VisibleIndex, "Id").ToString(), out id);
        if (id > 0)
        {
          ckb.Attributes.Add("OnChange", "checkChange('" + id + "',this.checked)");
        }
        if (!CanModify) ckb.Disabled = true;
      
      }
    }

    protected void gvList_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
    {

     // GridViewCommandColumnButton deleteButton = e.Column.DeleteButton;
    //  if (deleteButton != null && !CanDelete) deleteButton.Visible = false;
      if (e.Button.ButtonType == ColumnCommandButtonType.Delete)
        if (!CanDelete) e.Visible = false;
      
    }

    protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "checkChange")
      {
        int id = 0;
        int.TryParse(para[1], out id);
        bool show = false;
        bool.TryParse(para[2], out show);
        m_innerError = ChangeStatus(id, show);
        LoadList();
      }
    }
    #endregion
  }
}
